package com.uk;

import com.uk.data.ejbs.IFaturaBean;
import com.uk.ui.fatura.FaturaPanel;
import com.uk.ui.kontrata.KontrataPanel;
import com.uk.ui.manaxhim.ManaxhimPanel;
import com.uk.ui.raporte.RaportePanel;
import com.uk.ui.tarifa.TarifaPanel;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class MainWindow extends Window implements ClickListener {
	private NativeButton kontrataButon = new NativeButton("Kontrata");
	private NativeButton tarifaButon = new NativeButton("Tarifa");
	private NativeButton faturaButon = new NativeButton("Fatura");
	private NativeButton manaxhimButon = new NativeButton("Manaxhim");
	private NativeButton raporteButton = new NativeButton("Raporte");
	private NativeButton helpButon = new NativeButton("Ndihme");
	private NativeButton logout = new NativeButton("Mbyll");
	
	private VerticalLayout layout = new VerticalLayout();
	private VerticalLayout mainLayout = new VerticalLayout();
	private KontrataPanel kontrataPanel;
	private TarifaPanel tarifaPanel;
	private FaturaPanel faturaPanel;
	private ManaxhimPanel manaxhimPanel;
	private RaportePanel raportePanel;
	
	

	private IFaturaBean faturaBean;
	
	
	public MainWindow(String caption, IFaturaBean faturaBean){
		
		System.out.println("MainWindow.MainWindow()");
		System.out.println(faturaBean);
		this.faturaBean = faturaBean;
		
		layout.setSizeFull();
		layout.addComponent(createToolbar());
		mainLayout.setSizeFull();
		layout.addComponent(mainLayout);
		layout.setExpandRatio(mainLayout, 1);
		this.setContent(layout);
		
	}
	
	
	private HorizontalLayout createToolbar() {
		HorizontalLayout lo = new HorizontalLayout();
		lo.addComponent(this.kontrataButon);
		lo.addComponent(this.tarifaButon);
		lo.addComponent(this.faturaButon);
		lo.addComponent(this.manaxhimButon);
		lo.addComponent(this.raporteButton);
		lo.addComponent(this.helpButon);
		lo.addComponent(this.logout);

		kontrataButon.addListener((ClickListener) this);
		tarifaButon.addListener((ClickListener) this);
		faturaButon.addListener((ClickListener) this);
		manaxhimButon.addListener((ClickListener)this);
		raporteButton.addListener((ClickListener)this);
		helpButon.addListener((ClickListener)this);
		logout.addListener((ClickListener) this);

		kontrataButon.setIcon(new ThemeResource("icons/32/folder-add.png"));
		tarifaButon.setIcon(new ThemeResource("icons/32/document-edit.png"));
		faturaButon.setIcon(new ThemeResource("icons/32/document-txt.png"));
		manaxhimButon.setIcon(new ThemeResource("icons/32/tools.png"));
		raporteButton.setIcon(new ThemeResource("icons/32/document-pdf.png"));
		helpButon.setIcon(new ThemeResource("icons/32/help.png"));
		logout.setIcon(new ThemeResource("icons/32/cancel.png"));

		lo.setMargin(true);
		lo.setSpacing(true);

		lo.setStyleName("toolbar");

		lo.setWidth("100%");

		Embedded em = new Embedded("", new ThemeResource("images/logo.png"));
		lo.addComponent(em);
		lo.setComponentAlignment(em, Alignment.MIDDLE_RIGHT);
		lo.setExpandRatio(em, 1);

		return lo;
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		final Button source = event.getButton();
		if (source == logout) this.closeApplication();
		else if (source == this.kontrataButon) {
			this.showKontrataPanel();
		}
		else if (source == this.faturaButon) this.showFaturaPanel();
		else if (source == this.tarifaButon) this.showTarifaPanel();
		else if (source == this.manaxhimButon) this.showManaxhimPanel();
		else if (source == this.raporteButton) this.showRaportePanel();
		
	}
	
	private void showManaxhimPanel() {
		try {
			if (this.mainLayout.getComponent(0) == this.manaxhimPanel) return;
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		this.resetPanels();
		this.manaxhimPanel = new ManaxhimPanel(faturaBean);
		this.mainLayout.addComponent(this.manaxhimPanel);
		
	}

	private void showRaportePanel(){
		try {
			if (this.mainLayout.getComponent(0) == this.raportePanel) return;
		} catch (Exception e) {
			// TODO: handle exception
		}
		this.resetPanels();
		this.raportePanel = new RaportePanel(faturaBean);
		this.mainLayout.addComponent(this.raportePanel);
	}

	private void closeApplication(){
		getWindow().showNotification("Mbyllje sesioni", "Sesioni eshte mbyllur", Notification.TYPE_HUMANIZED_MESSAGE);
		((FaturaUKApplication)getWindow().getApplication()).loadLoginLayout();
	}
	
	private void showFaturaPanel(){
		try {
			if (this.mainLayout.getComponent(0) == this.faturaPanel) return;
		} catch (Exception e) {
			// TODO: handle exception
		}
		this.resetPanels();
		this.faturaPanel = new FaturaPanel(faturaBean);
		this.mainLayout.addComponent(this.faturaPanel);
	}
	
	private void showTarifaPanel(){
		try {
			if (this.mainLayout.getComponent(0) == this.tarifaPanel) return;
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		this.resetPanels();
		this.tarifaPanel = new TarifaPanel(faturaBean);
		this.mainLayout.addComponent(this.tarifaPanel);
	}
	
	private void showKontrataPanel(){
		
		try {
			if (this.mainLayout.getComponent(0) == this.kontrataPanel) return;
		}catch (IndexOutOfBoundsException e) {
			
		}
		
		this.resetPanels();
		this.kontrataPanel = new KontrataPanel(faturaBean);
		this.mainLayout.addComponent(this.kontrataPanel);
		//this.mainLayout.setExpandRatio(this.kontrataPanel, 1);
	}
	
	private void resetPanels(){
		this.mainLayout.removeAllComponents();
		this.kontrataPanel = null;
		this.tarifaPanel = null;
		this.faturaPanel = null;
		this.manaxhimPanel = null;
		this.raportePanel = null;
	}
	
	
}
